package com.sl.leetcode;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Stack;

/**
 * Created by lisulong on 2019/5/28.
 */
public class Solution20 {
    public boolean isValid1(String s) {
        final char leftS = '(';
        final char leftM = '[';
        final char leftX = '{';
        final char rightS = ')';
        final char rightM = ']';
        final char rightX = '}';

        char[] sCharArr = s.toCharArray();
        Stack<Character> charStack = new Stack<>();
        for (char c : sCharArr) {
            switch (c) {
                case leftM:
                case leftS:
                case leftX:
                    charStack.push(c);
                    break;
                case rightS:
                    if (charStack.pop() != leftS) return false;
                    break;
                case rightM:
                    if (charStack.pop() != leftM) return false;
                    break;
                case rightX:
                    if (charStack.pop() != leftX) return false;
                    break;
            }
        }
        return true;
    }
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<Solution20> clz = Solution20.class;
        Object obj = clz.newInstance();
        Method executeMethod;
        for (int i = 1; i <= 1; i++) {
            long startTime = System.nanoTime();
            executeMethod = clz.getMethod("isValid" + i, String.class);
            System.out.println(executeMethod.invoke(obj, "{([]])}"));//数组做为参数的，需要使用new Object[]{}包一层
            System.out.println("====耗时：" + (System.nanoTime() - startTime));
        }
    }
}
